//
//  SchoolDetailCommentTableViewCell.swift
//  HoneyBee
//
//  Created by point on 2018/6/2.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import SwiftyJSON

private let cellID = "cellID"


@objc protocol  SchoolDetailCommentTableViewCellDelegate : class {
    @objc optional func commentReloadHeight(index:IndexPath)
    @objc optional func commentShowImgView(index:IndexPath , ImgIndex:IndexPath)
}


class SchoolDetailCommentTableViewCell: UITableViewCell {
    weak var delegate: SchoolDetailCommentTableViewCellDelegate?
    
    fileprivate lazy var headImg:UIImageView =  UIImageView()
    fileprivate lazy var nameLabel:UILabel =  UILabel(UIColor.black, 15, "老鼠爱大米")
    fileprivate lazy var timeLabel:UILabel =  UILabel(UIColor.black, 12, "2016.09.12")
    
    fileprivate lazy var contentLabel:UILabel =  UILabel(UIColor.black, 15, "sdfsdfs鼠爱大米dfsdfs鼠爱大米dfsdsdf鼠爱大米sdfdsfds鼠爱大米fsdfsdfsdfsdfsdfs鼠爱大米dfsdfs鼠爱大米dfsdsdf鼠爱大米sdfdsfds鼠爱大米fsdfsdfsdsdfsdfs鼠爱大米dfsdfs鼠爱大米dfsdsdf鼠爱大米sdfdsfds鼠爱大米fsdfsdfsdsdfsdfs鼠爱大米dfsdfs鼠爱大米dfsdsdf鼠爱大米sdfdsfds鼠爱大米fsdfsdfsd")
    
    fileprivate var curModel:SchoolDetailCommentModel!
    
    fileprivate var currentIndex:IndexPath!
    
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 7
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(DCCustomAlbumCollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        collectionView.isPagingEnabled = false
        
        collectionView.scrollsToTop = false
        collectionView.showsHorizontalScrollIndicator = false
        
        return collectionView
    }()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



// MARK:- 显示数据
extension SchoolDetailCommentTableViewCell {
    func configModel( model: [SchoolDetailCommentModel], index:IndexPath) {
        currentIndex = index
        curModel = model[index.item]
        contentLabel.text = curModel.content
        
        if  curModel.user != nil {
            if curModel.user.headImg.count > 0 {
                headImg.showImageI(curModel.user.headImg)
            }else {
                headImg.image = UIImage(named: "group_leader")
            }
            nameLabel.text = curModel.user.userName
        }
        
     
        
        let time = String.stringToTimeStamp(stringTime: curModel.createTime)
        
        timeLabel.text = time
        if curModel.cellHeight == 0 {
            contentView.layoutIfNeeded()
            curModel.cellHeight = contentLabel.height == 0 ? 0.01 : contentLabel.height
            delegate?.commentReloadHeight!(index: index)
        }
        
        self.collectionView.isHidden = curModel.resource.count == 0
        
        self.collectionView.reloadData()
    }
}

// MARK: - 代理
extension SchoolDetailCommentTableViewCell: UICollectionViewDelegate , UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return curModel.resource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! DCCustomAlbumCollectionViewCell
        cell.configModel(model: curModel.resource[indexPath.item], index: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.commentShowImgView!(index: currentIndex , ImgIndex: indexPath)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 60, height: 60)
    }
}

extension SchoolDetailCommentTableViewCell {
    
    fileprivate func setupUI(){
        contentView.addSubview(headImg)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(collectionView)
        contentView.backgroundColor = UIColor.white
        collectionView.backgroundColor = UIColor.white
        
        headImg.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 20, height: 20))
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
        }
        headImg.backgroundColor = kColorC1
        headImg.fastCornerRadius(value: 10)
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImg.snp.right).offset(10)
            make.centerY.equalTo(headImg)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.right.equalTo(contentView).inset(10)
            make.centerY.equalTo(headImg)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(20)
            make.right.equalToSuperview().inset(20)
        }
        
        contentLabel.numberOfLines = 0
        collectionView.snp.makeConstraints { (make) in
            make.height.equalTo(60)
            make.left.equalTo(nameLabel.snp.left)
            make.right.equalToSuperview().inset(20)
            make.bottom.equalToSuperview().inset(20)
        }
    }
}
